package com.sise.buffer;

import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * @author idea
 * @data 2020/4/6
 */
public class BufferDemo {

    public static void main(String[] args) throws IOException {
        RandomAccessFile randomAccessFile = new RandomAccessFile("test.txt", "rw");
        FileChannel fileChannel = randomAccessFile.getChannel();
        ByteBuffer byteBuffer = ByteBuffer.allocate(1024);
        int byteRead = fileChannel.read(byteBuffer);
        while (byteRead != -1) {
            System.out.print("read content is :" + byteRead);
            byteBuffer.flip();
            while (byteBuffer.hasRemaining()) {
                System.out.print((char)(byteBuffer.get()));
            }
            byteBuffer.clear();
            byteRead = fileChannel.read(byteBuffer);
        }
        randomAccessFile.close();
    }
}
